package com.local.web.service.impl;

import com.local.common.constants.BusinessException;
import com.local.common.constants.ExceptionCodeEnum;
import com.local.common.util.ResultUtil;
import com.local.user.feign.UserServiceFeign;
import com.local.user.pojo.User;
import com.local.user.pojo.UserLoginRequestParam;
import com.local.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserServiceFeign userServiceFeign;

    @Override
    public User getByLoginNamePassword(UserLoginRequestParam requestParam, HttpSession session) {
        ResultUtil<User> userResult = userServiceFeign.getByLoginNamePassword(requestParam);
        if (Objects.isNull(userResult)) {
            throw new BusinessException(ExceptionCodeEnum.BUSINESS_EXCEPTION.getCode(), ExceptionCodeEnum.BUSINESS_EXCEPTION.getMessage());
        }
        if (userResult.getCode() != 200) {
            throw new BusinessException(userResult.getCode(), userResult.getMessage());
        }
        User user = userResult.getData();
        return user;
    }


    /**
     * 更新用户地址
     * @param user
     * @return
     */
    public ResultUtil updateAddress(User user) {
        ResultUtil result = userServiceFeign.updateAddress(user);
        return result;
    }
}
